package com.yxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxy.common.PageResult;
import com.yxy.constant.WantOrSeeConstant;
import com.yxy.context.BaseContext;
import com.yxy.mapper.BookMapper;
import com.yxy.mapper.FilmMapper;
import com.yxy.mapper.WantOrSeeMapper;
import com.yxy.pojo.dto.TypeDTO;
import com.yxy.pojo.dto.PageWantOrSeeQueryDTO;
import com.yxy.pojo.dto.UserDTO;
import com.yxy.pojo.dto.WantOrSeeDTO;
import com.yxy.pojo.entity.WantOrSee;
import com.yxy.pojo.vo.BookDetailVO;
import com.yxy.pojo.vo.FilmDetailVO;
import com.yxy.pojo.vo.UserWantOrSeeVO;
import com.yxy.pojo.vo.WantOrSeeVO;
import com.yxy.service.WantOrSeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class WantOrSeeServiceImpl implements WantOrSeeService {

    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private FilmMapper filmMapper;
    @Autowired
    private WantOrSeeMapper wantOrSeeMapper;

    /**
     * 查询用户对图书的想看和看过的状态
     * 初始化想看或看过的状态（仅限每个类型中的模块第一次进入时设置，其余均只用修改值即可）
     * @param typeDTO
     * @return
     */
    @Override
    public WantOrSeeVO getWantOrSeeStatus(TypeDTO typeDTO) {
        WantOrSeeVO vo = new WantOrSeeVO();
        // 先查询是否有数据，有就直接返回数据，没有则初始化后再返回数据
        WantOrSee wantOrSee = wantOrSeeMapper.select(typeDTO);
        if (wantOrSee == null) {
            // 新增操作
            WantOrSee build = WantOrSee.builder()
                    .type(typeDTO.getType())
                    .typeId(typeDTO.getTypeId())
                    .userId(typeDTO.getUserId())
                    .createTime(LocalDate.now())
                    .wantStatus(WantOrSeeConstant.INIT_WANT_STATUS)
                    .seeStatus(WantOrSeeConstant.INIT_SEE_STATUS)
                    .build();
            wantOrSeeMapper.initStatus(build);
            // 查询新的数据再返回
            wantOrSee = wantOrSeeMapper.select(typeDTO);
        }
        BeanUtils.copyProperties(wantOrSee, vo);
        return vo;
    }


    /**
     * 获取用户想看或看过的数据
     * @param pageWantOrSeeQueryDTO
     * @return
     */
    @Override
    public PageResult getUserWantOrSee(PageWantOrSeeQueryDTO pageWantOrSeeQueryDTO) {
        // 用mybatis的分页插件进行分页，设置当前页为pageWantOrSeeQueryDTO.getPage()，每页显示条目为pageWantOrSeeQueryDTO.getPageSize()
        PageHelper.startPage(pageWantOrSeeQueryDTO.getPage(), pageWantOrSeeQueryDTO.getPageSize());
        // 根据用户id和想看或看过的状态查询数据
        WantOrSee wantOrSee = WantOrSee.builder()
                .userId(pageWantOrSeeQueryDTO.getUserId())
                .wantStatus(pageWantOrSeeQueryDTO.getWantStatus())
                .seeStatus(pageWantOrSeeQueryDTO.getSeeStatus())
                .build();
        Page<WantOrSee> page = wantOrSeeMapper.selectList(wantOrSee);
        // 存放返回前端的集合
        List<UserWantOrSeeVO> list = new ArrayList<>();
        page.getResult().forEach(wantOrSeeItem -> {
            UserWantOrSeeVO vo = new UserWantOrSeeVO();
            if(wantOrSeeItem.getType().equals("book")){
                BookDetailVO bookDetailVO = bookMapper.selectById(wantOrSeeItem.getTypeId());
                vo.setType("book");
                vo.setTypeId(bookDetailVO.getId());
                vo.setTitle(bookDetailVO.getTitle());
                vo.setCover(bookDetailVO.getCover());
            }else if(wantOrSeeItem.getType().equals("film")){
                FilmDetailVO filmDetailVO = filmMapper.selectById(wantOrSeeItem.getTypeId());
                vo.setType("film");
                vo.setTypeId(filmDetailVO.getId());
                vo.setTitle(filmDetailVO.getTitle());
                vo.setCover(filmDetailVO.getCover());
            }
            list.add(vo);
        });
        return new PageResult(page.getTotal(), list);
    }

    /**
     * 更新用户对图书的想看和看过的状态
     *
     * @param wantOrSeeDTO
     * @return
     */
    @Override
    public void updateStatus(WantOrSeeDTO wantOrSeeDTO) {
        WantOrSee wantOrSee = WantOrSee.builder()
                .type(wantOrSeeDTO.getType())
                .typeId(wantOrSeeDTO.getTypeId())
                .wantStatus(wantOrSeeDTO.getWantStatus())
                .seeStatus(wantOrSeeDTO.getSeeStatus())
                .build();
        Integer userId = BaseContext.getCurrentId();
        wantOrSee.setUserId(userId);
        wantOrSee.setUpdateTime(LocalDate.now());
        wantOrSeeMapper.updateStatus(wantOrSee);
    }
}
